package com.danger.Presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import com.danger.Model.action.ExtractAudioModel;
import com.danger.Model.action.ExtractAudioModelImpl;
import com.danger.Model.bean.VideoBean;
import com.danger.Model.listener.OnCommonListener;
import com.danger.Model.listener.OnExtractAudioListener;
import com.danger.Model.listener.OnGetThumbListener;
import com.danger.Util.FileUtil;
import com.danger.View.ExtractAudioView;

/**
 * Created by danger on 2016/4/13.
 */
public class ExtractAudioPresenterImpl implements ExtractAudioPresenter {

    private Context context;
    private ExtractAudioView view;
    private ExtractAudioModel model;

    public ExtractAudioPresenterImpl(Context context, ExtractAudioView view) {
        this.context = context;
        this.view = view;
        model = new ExtractAudioModelImpl(context);
    }

    @Override
    public void extractAudio(final VideoBean videoBean) {
        view.showDialog("正在提取...");
        if (videoBean != null) {
            model.getAudio(videoBean, new OnExtractAudioListener() {
                @Override
                public void onSuccess(String savePath) {
                    view.hideDialog();
                    view.showSuccess(savePath, videoBean.getTitle());
                }

                @Override
                public void onError(String error) {
                    view.hideDialog();
                    view.showError(error);
                }
            });
        } else {
            view.hideDialog();
            view.showError("找不到对应的视频");
        }
    }

    @Override
    public void setThumb(VideoBean videoBean) {
        model.getThumb(videoBean.getDate(), new OnGetThumbListener() {
            @Override
            public void onSuccess(Bitmap bitmap, String msg) {
                view.getImageView().setImageBitmap(bitmap);
                view.showSuccess(null, msg);
            }

            @Override
            public void onError(String error) {
                view.showError(error);
            }
        });
    }

    @Override
    public void getAudioContent(final String path, final String name, String rate, String type) {
        view.showDialog("正在获取文本...");
        model.getContentByNet(path, rate, type, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                FileUtil.writeTxtToFile(msg, Environment.getExternalStorageDirectory() + "/vitamio" + "/content/", name + ".txt");
                view.hideDialog();
                view.showSuccess(path, "获取音频识别内容成功" + msg);
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError("获取音频识别内容失败" + error);
            }
        });
    }
}
